from django.contrib.auth.models import AbstractUser
from uuid import uuid4
from django.db import models


class DoorStatus(models.Model):
    # 0 == 已关门
    # 1 == 已开门
    type = models.IntegerField(null=True, verbose_name='寝室门状态')

    class Meta:
        db_table = 'door_status'
        verbose_name = '状态表'
        verbose_name_plural = verbose_name


class Room(models.Model):
    number = models.CharField(max_length=5, null=True, verbose_name='寝室号')
    position = models.CharField(max_length=50, null=True, verbose_name='经纬度')
    door_status = models.ForeignKey(to='DoorStatus', null=True, verbose_name='所属寝室门状态', on_delete=models.CASCADE)

    class Meta:
        db_table = 'rooms'
        verbose_name = '寝室'
        verbose_name_plural = verbose_name


class User(AbstractUser):
    user_secret = models.UUIDField(default=uuid4(), null=True, verbose_name='用户JWT秘钥')
    user_jwt = models.CharField(max_length=300, null=True, verbose_name='JWT')
    # 所属寝室号
    room = models.ForeignKey(to='Room', null=True, verbose_name='所属寝室号', on_delete=models.CASCADE)

    class Meta:
        db_table = 'users'
        verbose_name = '用户'
        verbose_name_plural = verbose_name


class MqttACL(models.Model):
    allow = models.IntegerField(max_length=1, null = True, verbose_name='0: deny, 1: allow')
    ipaddr = models.CharField(max_length=60, null=True, verbose_name='ip地址')
    username = models.CharField(max_length=100, null=True, verbose_name='用户名')
    clientid = models.CharField(max_length=100, null=True, verbose_name='客户端id')
    access = models.IntegerField(max_length=2, null = True, verbose_name='1: subscribe, 2: publish, 3: pubsub')
    topic = models.CharField(max_length=100, null=True, verbose_name='话题')

    class Meta:
        db_table = 'mqtt_acl'
        verbose_name = 'mqtt_acl'
        verbose_name_plural = verbose_name


